package com.wzapp.sitposture.task.repository;

import com.wzapp.sitposture.task.entity.Task;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by qianglong on 2018/1/23.
 */
@RepositoryRestResource(exported = false)
public interface TaskRepository extends JpaRepository<Task,String> {

    /**
     * 功能: 根据定时任务名称和状态查询定时任务<br/>
     *
     * @author ql
     * @create 2018-05-08 11:31
     * @param name
     * @param status
     * @return java.util.List<Task>
     **/
    List<Task> findByNameAndStatus(String name, Integer status);

    /**
     * 功能: 根据任务id集合更新定时任务状态<br/>
     *
     * @author ql
     * @create 2018-05-08 11:31
     * @param ids
     * @return void
     **/
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    @Query(value = "update task set status = 2 ,update_time = now() where id in (?1) ",nativeQuery = true)
    void updateTaskStatus(List<String> ids);

    /**
     * 功能: 根据创建人和任务名称和状态查询定时任务<br/>
     *
     * @author ql
     * @create 2018-05-08 11:32
     * @param creator
     * @param name
     * @param status
     * @return Task
     **/
    Task findByCreatorAndNameAndStatus(String creator,String name, Integer status);
}
